package com.funymmz.demo.Controller;

import com.funymmz.demo.domain.User;
import com.funymmz.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 返回登录页面
     * @param httpServletRequest http请求
     * @param model 模板
     * @return 登录页面
     */
    @RequestMapping("/login")
    public String list(HttpServletRequest httpServletRequest, Model model){

        HttpSession session = httpServletRequest.getSession();
        if (session.getAttribute("userid") == null){
            return "login";
        }
        List<User> users =userService.findAll();

        model.addAttribute("data", users);

        return "home";
    }

    /**
     * 主页
     * @return 主页页面
     */
    @RequestMapping("/home")
    public String home() {
        return "home";
    }

    /**
     * 注册页面
     * @return 注册页面
     */
    @RequestMapping("/register")
    public String register() {
        return "register";
    }

    /**
     * 管理员页面
     * @return 管理员页面
     */
    @RequestMapping("/admin/user")
    public String admin() {
        return "user";
    }
}
